using System.Data;
using System.IO;
using NHibernate;
using NHibernate.Search;
using NHibernate.Search.Engine;
using NHibernate.Search.Impl;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

namespace EasyCMMS.Tests.RepositoryTests
{
    public abstract class NHibernateSearchTestFixture<T> : NHibernateInMemoryTestFixtureBase 
    {

        protected IFullTextSession session;

        protected abstract string IndexPath { get; }


        [TestFixtureSetUp]
        public void OneTimeTestInitialize()
        {
            //clear index
            if(Directory.Exists(IndexPath ))
                Directory.Delete(IndexPath,true);
            
            OneTimeInitalize(typeof(T).Assembly);
        }



        [SetUp]
        public void TestInitialize()
        {
            session = this.CreateFullTextSession();
        }



        [TearDown]
        public void TestCleanup()
        {
            session.Dispose();

        }


        private IFullTextSession CreateFullTextSession()
        {

           
            SearchFactory.Initialize(configuration, sessionFactory);
            ISession openSession = sessionFactory.OpenSession(new SearchInterceptor());

            IDbConnection connection = openSession.Connection;

            new SchemaExport(configuration).Execute(false, true, false, true, connection, null);

            return Search.CreateFullTextSession(openSession);


        } 
    }
}